package com.mojo.io.buffer;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * 缓冲流示例文件/图片/视频文件的复制
 * <p>
 * FileInputStream/FileOutputStream
 * BufferedInputStream/BufferedOutputStream
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/12/2 18:36
 */
public class FileStreamDemo {
    public static void main(String[] args) {
        //fileInOutputStreamCopy();
        //bufferedInOutputStreamCopy();
        // imageEncryption();
        // 获取文件中相同的字符及次数
        getFileChar();
    }

    private static void getFileChar() {
        File file = new File("E:\\技术文档\\短信发送\\瑞动科技短信短信接口调用说明书.doc");

        try (InputStream inputStream = Files.newInputStream(file.toPath());
             BufferedWriter writer = new BufferedWriter(new FileWriter("E:\\技术文档\\短信发送\\b.txt"))) {

            int n;
            HashMap<Character, Integer> map = new HashMap<>(64);
            while ((n = inputStream.read()) != -1) {
                Character c = (char) n;
                map.merge(c, 1, Integer::sum);
            }
            System.out.println("map的size>>:" + map.size());
            for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                Character key = entry.getKey();
                switch (key) {
                    case ' ':
                        writer.write("空格=" + entry.getValue());
                        break;
                    case '\t':
                        writer.write("tab=" + entry.getValue());
                        break;
                    case '\r':
                        writer.write("回车=" + entry.getValue());
                        break;
                    case '\n':
                        writer.write("换行=" + entry.getValue());
                        break;
                    default:
                        writer.write(key + "=" + entry.getValue());
                        break;
                }
                writer.newLine();
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    //图片加密
    private static void imageEncryption() {
        try (FileInputStream inputStream = new FileInputStream("C:\\Users\\masqu\\Desktop\\星云1.jpg");
             FileOutputStream outputStream = new FileOutputStream("C:\\Users\\masqu\\Desktop\\星云2.jpg")) {
            byte[] b = new byte[16];
            int n = 0;
            while ((n = inputStream.read(b)) > -1) {
                // 循环对字节加密/解密，异或运算
                for (int i = 0; i < b.length; i++) {
                    b[i] = (byte) (b[i] ^ 5);
                }
                outputStream.write(b, 0, n);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // BufferedInputStream/BufferedOutputStream复制文件
    private static void bufferedInOutputStreamCopy() {
        File file = new File("e:\\ebook\\JAVA性能优化权威指南.pdf");
        try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()));
             BufferedOutputStream outputStream =
                     new BufferedOutputStream(Files.newOutputStream(Paths.get(file.getParent() + File.separator + "Java 并发编程实战_copy1.pdf")))
        ) {

            byte[] b = new byte[8092];
            int n;
            // 读取byte数据
            long start = System.currentTimeMillis();
            while ((n = inputStream.read(b)) > 0) {
                //输出
                outputStream.write(b, 0, n);
            }
            long end = System.currentTimeMillis();
            System.out.println("BufferedInputStream/BufferedOutputStream复制耗时(毫秒)>>:" + (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //FileInputStream/FileOutputStream复制文件
    private static void fileInOutputStreamCopy() {
        File file = new File("e:\\ebook\\JAVA性能优化权威指南.pdf");
        try (FileInputStream inputStream = new FileInputStream(file);
             FileOutputStream outputStream = new FileOutputStream(file.getParent() + File.separator + "Java 并发编程实战_copy.pdf")) {
            byte[] b = new byte[8092];
            int n;
            // 读取byte数据
            long start = System.currentTimeMillis();
            while ((n = inputStream.read(b)) > 0) {
                //输出
                outputStream.write(b, 0, n);
            }
            long end = System.currentTimeMillis();
            System.out.println("FileInputStream/FileOutputStream复制耗时(毫秒)>>:" + (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
